package com.dbkj.common.handler;

import com.jfinal.handler.Handler;
import com.jfinal.render.RenderManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CORSHandler extends Handler {
    private static final RenderManager r = RenderManager.me();
    @Override
    public void handle(String s, HttpServletRequest request, HttpServletResponse response, boolean[] booleans) {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age","3600*4");

        if(request.getMethod().equals("OPTIONS")){
            r.getRenderFactory().getJsonRender().setContext(request,response).render();
        } else if(request.getRequestURI().contains("sockjs")){
            r.getRenderFactory().getJsonRender().setContext(request,response).render();
        } else{
            next.handle(s,request,response,booleans);
        }
    }
}
